#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 105;

int n, m;
int a[N][N];
int sum[N][N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, m) cin >> a[i][j];
  rep(i, 1, n) rep(j, 1, m) {
    sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + a[i][j];
  }
  per(len, min(n, m), 1) {
    int area = len * len;
    rep(i, len, n) rep(j, len, m) {
      int now =
          sum[i][j] - sum[i - len][j] - sum[i][j - len] + sum[i - len][j - len];
      if (now == area) {
        cout << len;
        return 0;
      }
    }
  }
  cout << 0;
  return 0;
}